package com.iims.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.utils.SeqNumUtils;
import com.iims.project.domain.CmisWorkToken;
import com.iims.project.mapper.CmisWorkTokenHelperMapper;
import com.iims.project.mapper.CmisWorkTokenMapper;
import com.iims.project.service.IWorkTokenService;
import com.iims.project.utils.WorkTokenCodeGenerator;
import com.iims.project.vo.query.GroupWorkTokenSearchQuery;
import com.iims.project.vo.request.WorkTokenEditRequest;
import com.iims.project.vo.resp.CmisWorkTokenDetailVo;
import com.iims.project.vo.resp.CmisWorkTokenRowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fanpei
 * @date 2021/7/8 11:03
 */
@Service
public class WorkTokenServiceImpl extends ServiceImpl<CmisWorkTokenMapper, CmisWorkToken> implements IWorkTokenService {

    @Autowired
    private CmisWorkTokenHelperMapper workTokenHelperMapper;

    @Override
    public List<CmisWorkTokenRowVo> getWorkTokenList(CmisWorkToken workToken) {
        List list = baseMapper.selectWorkTokenList(workToken);
        return SeqNumUtils.initSeqNum(workToken, list);
    }

    @Override
    public List<CmisWorkTokenRowVo> getWorkTokenListByGroup(GroupWorkTokenSearchQuery groupSearchQuery) {
        groupSearchQuery.generateSqlArgs();
        List list = baseMapper.selectWorkTokenListByGroup(groupSearchQuery);
        return SeqNumUtils.initSeqNum(groupSearchQuery, list);
    }

    @Override
    public CmisWorkTokenDetailVo getWorkTokenDetailById(Long workTokenId) {
        CmisWorkTokenDetailVo result = new CmisWorkTokenDetailVo();
        CmisWorkTokenRowVo cmisWorkToken = baseMapper.selectWorkTokenDetail(workTokenId);
        BeanUtil.copyProperties(cmisWorkToken, result);
        result.setHelpers(workTokenHelperMapper.selectWorkTokenHelpers(workTokenId));
        return result;
    }

    @Override
    public List<CmisWorkTokenRowVo> getProWorkTokenList(Long proId) {
        return baseMapper.selectProWorkTokenList(proId);
    }

    @Override
    public List<CmisWorkTokenRowVo> getContractWorkTokenList(Long contractId) {
        return baseMapper.selectContractWorkTokenList(contractId);
    }

    @Override
    public void insertWorkToken(WorkTokenEditRequest workToken) {
        String proCode = WorkTokenCodeGenerator.getWorkTokenCode(workToken);
        workToken.setWorkTokenCode(proCode);
        baseMapper.insert(workToken);
        if (ObjectUtil.isNotEmpty(workToken.getHelpers())) {
            workTokenHelperMapper.batchInsert(workToken.getWorkTokenId(), workToken.getHelpers());
        }
    }

    @Override
    public void updateWorkToken(WorkTokenEditRequest workToken) {
        LambdaQueryWrapper<CmisWorkToken> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .select(CmisWorkToken::getWorkTokenCode)
                .eq(CmisWorkToken::getWorkTokenId, workToken.getWorkTokenId());
        CmisWorkToken OLD = baseMapper.selectOne(queryWrapper);
        //排除workTokenCode
        workToken.setWorkTokenCode(OLD.getWorkTokenCode());
        baseMapper.updateById(workToken);
        if (ObjectUtil.isNotEmpty(workToken.getHelpers())) {
            //先删除所有协作者再更新
            deletAllWorkTokenHelpers(workToken.getWorkTokenId());
            workTokenHelperMapper.batchInsert(workToken.getWorkTokenId(), workToken.getHelpers());
        }
    }

    @Override
    public void deleteById(Long workTokenId) {
        baseMapper.deleteById(workTokenId);
        deletAllWorkTokenHelpers(workTokenId);
    }

    /**
     * 删除所有协作者
     *
     * @param workTokenId
     */
    private void deletAllWorkTokenHelpers(Long workTokenId) {
        Map<String, Object> map = new HashMap<>();
        map.put("work_token_id", workTokenId);
        workTokenHelperMapper.deleteByMap(map);
    }


    @Override
    public boolean checkCmisWorkTokenUse(Long workTokenId) {
        return baseMapper.selectWorkTokenUseCount(workTokenId) > 0;
    }
}